package com.hph.health.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ErrorCode;
import com.hph.health.common.ResultUtils;
import com.hph.health.exception.BusinessException;
import com.hph.health.model.dto.user.DeleteRequest;
import com.hph.health.model.entity.ProductPicInfo;
import com.hph.health.service.ProductPicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 商品图片接口
 *
 * @author hanph
 * @from haut
 */
@RestController
@RequestMapping("/productpic")
@Slf4j
public class ProductPicInfoController {

    @Resource
    private ProductPicInfoService productPicInfoService;

    /**
     * 获取图片
     *
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<ProductPicInfo>> listProductByPage(long id) {
        QueryWrapper<ProductPicInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", id);
        List<ProductPicInfo> productPicInfoList = productPicInfoService.list(queryWrapper);
        return ResultUtils.success(productPicInfoList);
    }

    /**
     * 删除图片
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProduct(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(productPicInfoService.removeById(deleteRequest.getId()));
    }

}
